package com.caishi.lkx.exam.util;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.caishi.lkx.common.Sensitive;
import com.caishi.lkx.exam.model.QuestionModel;
import com.caishi.lkx.exam.service.IQuestionService;
import com.zzw.common.SpringContextHolder;

import com.caishi.lkx.exam.QuestionCollectionBlockItem;
import com.caishi.lkx.exam.ienum.type.QuestionSourceType;


import java.util.Collection;
import java.util.List;
import java.util.Map;

public class QuestionUtil {

    private QuestionUtil() {
    }

    private static final IQuestionService questionService;

    static {
        questionService = SpringContextHolder.getBean(IQuestionService.class);
    }


    /**
     * 判断试题id是否时组合题
     *
     * @param qid 试题id
     * @return
     */
    public static boolean questionIdIsCombination(String qid) {
        return questionIdIsCombinationParent(qid) || questionIdIsCombinationChild(qid);
    }

    /**
     * 判断试题id是否时组合题的主题
     *
     * @param qid 试题id
     * @return
     */
    public static boolean questionIdIsCombinationParent(String qid) {
        return qid.endsWith("_0");
    }

    /**
     * 判断试题id是否时组合题的子题
     *
     * @param qid 试题id
     * @return
     */
    public static boolean questionIdIsCombinationChild(String qid) {
        return qid.endsWith("_1");
    }

    public static Integer getCollectionQuestionCount(List<QuestionCollectionBlockItem> data) {
        if (data == null) return null;
        return data.stream().mapToInt(v -> {
            var a = v.getQs().stream().filter(v1 -> questionIdIsCombinationParent(v1.getId())).toList();
            if (a.isEmpty()) return v.getQs().size();
            var a1 = v.getQs().stream().filter(v1 -> !questionIdIsCombinationParent(v1.getId())).toList();
            return a1.size() +
                    a.stream().mapToInt(v1 -> Math.toIntExact(questionService.selectCount(
                            questionService.wrappers().eq(QuestionModel::getSourceType, QuestionSourceType.combination)
                                    .eq(QuestionModel::getSourceId, v1.getId())
                    ))).sum();
        }).sum();
    }

    /**
     * 获取试题总分
     *
     * @param data 数据集合
     * @return
     */
    public static Integer getCollectionQuestionScore(List<QuestionCollectionBlockItem> data) {
        if (data == null) return null;
        return data.stream().mapToInt(m -> m.getScore().intValue()).sum();
    }

    public static List<String> getCollectionQuestionAllIds(List<QuestionCollectionBlockItem> data) {
        if (data == null) return null;
        return data.stream()
                .map(QuestionCollectionBlockItem::getQs)
                .flatMap(Collection::stream)
                .map(QuestionCollectionBlockItem.QuestionCollectionItem::getId)
                .toList();
    }

    //过滤敏感词汇
//    public static List<Map<String, Object>> sensitiveStem(List<Map<String, Object>> stem) {
//        if (null == stem) return null;
//        JSONArray sensitive = Sensitive.getSensitive();
//        stem.forEach(s -> {
//            String v = (String) s.get("vl");
//            if (StrUtil.isBlank(v)) return;
//            String s1 = v.replaceAll("\\{\\{U}}\\s*\\{\\{/U}}", "").replaceAll("<u>\\s*</u>", "");
//            s.put("vl", s1);
//            sensitive.forEach(f -> {
//                String sen = f.toString();
//                StringBuilder filter = new StringBuilder();
//                filter.append("*".repeat(sen.length()));
//                if (s.get("vl") == null) return;
//                String vl = (String) s.get("vl");
//                if (StrUtil.isNotBlank(vl)) s.put("vl", vl.replaceAll(sen, filter.toString()));
//            });
//        });
//        return stem;
//    }
//
//    public static List<Map<String, Object>> sensitiveQuOption(List<Map<String, Object>> quOption) {
//        if (null == quOption) return null;
//        JSONArray sensitive = Sensitive.getSensitive();
//        quOption.forEach(s -> {
//            sensitive.forEach(f -> {
//                String sen = f.toString();
//                String jsonString = JSONArray.toJSONString(s.get("content"));
//                String contentT = jsonString.replaceAll(sen, "*".repeat(sen.length()));
//                try {
//                    JSONArray jsonArray = JSONArray.parseArray(contentT);
//                    s.put("content", jsonArray);
//                } catch (JSONException e) {
//
//                }
//            });
//        });
//        return quOption;
//    }

    public static List<Map<String, Object>> sensitiveContent(List<Map<String, Object>> content) {
        if (CollUtil.isEmpty(content)) return content;
        var json = Sensitive.replace(JSONArray.toJSONString(content));
        return JSONArray.parseArray(json).stream().map(v -> (Map<String, Object>) v).toList();
    }
}
